package com.zhang.goods.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.goods.dto.SpuSearchDto;
import com.zhang.goods.entity.SpuInfo;
import com.zhang.goods.mapper.SpuInfoMapper;
import com.zhang.goods.service.ISpuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhang.system.service.IAdminUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * spu商品信息表 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2023-10-04
 */
@Service
@Slf4j
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo> implements ISpuInfoService {
    @Autowired
    private IAdminUserService adminUserService;

    @Override
    public Page<SpuInfo> getMySpuListPage(Integer pageNum, Integer pageSize, SpuSearchDto spuSearchDto) {
        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<>();
        boolean isAdmin = adminUserService.isAdmin(StpUtil.getLoginIdAsLong());
        if (!isAdmin) {
            log.info("不是超级管理员{}",StpUtil.getLoginIdAsLong());
            wrapper.eq("business_id", StpUtil.getLoginIdAsLong());
        }
        if (spuSearchDto != null) {
            if (!StrUtil.isBlank(spuSearchDto.getSearchValue())) {
                wrapper.eq("id", spuSearchDto.getSearchValue()).or().like("spu_name", spuSearchDto.getSearchValue());
            }
            if (spuSearchDto.getCategoryId() != null) {
                log.info("看看分类条件{}", spuSearchDto.getCategoryId());
                wrapper.eq("category_id", spuSearchDto.getCategoryId());
            }
            if (spuSearchDto.getBrandId() != null) {
                wrapper.eq("brand_id", spuSearchDto.getBrandId());
            }
            if (spuSearchDto.getState() != null) {
                wrapper.eq("state", spuSearchDto.getState());
            }
        }
        Page page = getSpuPageByQ(pageNum, pageSize, wrapper);
        return page;
    }

    @Override
    public Page<SpuInfo> getSpuPageByQ(Integer pageNum, Integer pageSize, QueryWrapper<SpuInfo> wrapper) {
        return page(new Page<SpuInfo>(pageNum, pageSize), wrapper);
    }

    @Override
    public List<SpuInfo> listFromUpStatus() {
        LambdaQueryWrapper<SpuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpuInfo::getState, 0);
        return list(wrapper);
    }
}
